package com.qf.day08.user.impl;

import com.qf.day08.user.service.UserService;
import com.qf.day08.user.user.User;

import java.util.Arrays;

public class UserServiceImpl implements UserService {

    private int count=0;
    private User userArr[]=new User[1];
    public User[] getUserArr() {
        return userArr;
    }

    public void setUserArr(User[] userArr) {
        this.userArr = userArr;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public boolean register(String userName,String passWord) {
        if (existFlag(userName)) {
            return false;
        }
        User user=new User(userName,passWord);
        userArr[count]=user;
        count++;
        if(count==userArr.length){
            userArr= Arrays.copyOf(userArr,userArr.length+5);
        }
        return true;
    }

    @Override
    public boolean login(String userName,String passWord) {
        //1. 遍历已经存在的用户对象，比对用户名和密码
        for (int i = 0; i < count; i++) {
            User user = userArr[i];
            if (user.getUserName().equals(userName) && user.getPassWord().equals(passWord) ) {
                return true;
            }
        }
        return false;
            }

    private boolean existFlag(String userName){
        for (int i=0;i<count;i++) {
            if(userName.equals(userArr[i].getUserName())) {
                //重名了
                return true;
            }

        }
        return false;
    }


}
